//
//  AppDelegate.m
//  School+
//
//  Created by Sean Sebastian on 4/22/16.
//  Copyright © 2016 Sean Sebastian. All rights reserved.
//

#import "AppDelegate.h"

#import "BYLoginControllerViewController.h"
#import "BYMainViewController.h"
#import "BYNewFeatureViewController.h"
#import "BYWelcomeViewController.h"

#import "BYLocationViewController.h"
#import "BYProfileTableViewController.h"

#import "BYAdminLoginViewController.h"
#import "BYRegisterViewController.h"
#import "BYAdminHomeViewController.h"

#import "BYVisitorViewController.h"

#import "UserAccount.h"

#import "CalendarViewController.h"

//#import <BaiduMapAPI_Map/BMKMapComponent.h>

@interface AppDelegate ()

@end

//BMKMapManager* _mapManager;
@implementation AppDelegate

/*
 界面切换：根据用户是否登录、根据是否是新版本判断跳转到不一样的控制器
 
 (UIApplication.sharedApplication().delegate 得到的就是AppDelegate，需要强转
 这样拿到AppDelegate就可以调用里面的方法了
 
 */

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.
    
    [self setupAppearance];
    
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    
    [[UserAccount sharedUserAccount] loadUserInfo];
    self.window.rootViewController = [self defaultController];
    
//    self.window.rootViewController = [[BYLocationViewController alloc] init];
    
    [self.window makeKeyAndVisible];
    
    
    NSLog(@"%@" , [UserAccount sharedUserAccount]);

    
    return YES;
}

- (void)setupAppearance
{
    [UINavigationBar appearance].tintColor = [UIColor grayColor];
    [UINavigationBar appearance].alpha = 0.5;
}

- (UIViewController *)defaultController
{
    // 判断是否登陆
    if ([[UserAccount sharedUserAccount] isSignIn]) {
        return [[BYWelcomeViewController alloc] init];
    }
    
    // 判断是否是新版本
    return [self isNewVersion]?[[BYNewFeatureViewController alloc] init] : [[BYVisitorViewController alloc] init];
}

- (void)switchRootController:(BOOL)isMain
{
    self.window.rootViewController = isMain ? [[BYVisitorViewController alloc] init] : [[BYWelcomeViewController alloc] init];
}

- (BOOL)isNewVersion
{
    // 获取当前版本号
    NSString *versionString = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
    double currentVersion = [versionString doubleValue];
    NSLog(@"当前版本号为：%.f" , currentVersion);
    
    // 获取之前的版本号
    NSString *sandboxVersionKey = @"sandboxVersionKey";
    double sandboxVersion = [[NSUserDefaults standardUserDefaults] doubleForKey:sandboxVersionKey];
    NSLog(@"沙盒版本号为：%.f" , sandboxVersion);
    
    // 保存当前版本
    [[NSUserDefaults standardUserDefaults] setDouble:currentVersion forKey:sandboxVersionKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    // 对比
    return currentVersion > sandboxVersion;
    
    // 调试新版本界面用
//    return YES;
}

- (void)toTheMain
{
    self.window.rootViewController = [[BYMainViewController alloc] init];
}


////////////////////////////////////////////////////////////////

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
